package com.qq2008.game.mojie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qq2008.game.mojie.entity.Message;
import com.qq2008.game.mojie.jdo.*;
import com.qq2008.game.mojie.mapper.BaseMonMapper;
import com.qq2008.game.mojie.redis.RedisUtil;
import com.qq2008.game.mojie.server.NpcServer;
import com.qq2008.game.mojie.server.SceneServer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

@Controller
@RequestMapping("/mojie")
public class HookController extends BaseController{

    @Resource
    RedisUtil redisUtil;

    @Resource
    private BaseMonMapper baseMonMapper;
    /**
     * 挂机
     */
    @RequestMapping("hook")
    public String hook(Model model, HttpSession session){
        Role role = getSessionRole(session);
        Room room = SceneServer.getRoom(role.getRoomId());
        model.addAttribute("room", room);
        int size = room.getNpcMap().size();
        model.addAttribute("roomNpcSize", size);
        time(model);
        return "hook";
    }

    @RequestMapping("/beHook")
    public String test(Model model, HttpSession session, RedirectAttributes attr) throws InterruptedException {
        Role role = getSessionRole(session);
        List<BaseMon> baseMons = baseMonMapper.selectList(new LambdaQueryWrapper<BaseMon>().eq(BaseMon::getRoomId, role.getRoomId()));
        Monster npc = NpcServer.getNpc(baseMons.get(0).getId());
        Room room = SceneServer.getRoom(npc.getRoomId());
        ExecutorService service1 = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            service1.submit(new Callable(){

                @Override
                public Object call() throws Exception {
                    System.out.println("当前线程:"+Thread.currentThread().getName());
                    Callable callable = new HookBattle(room, BattleType.PVE,role,npc);
                    FutureTask futureTask = new FutureTask(callable);
                    Thread t = new Thread(futureTask);
                    t.start();
                    return futureTask.get();
                }

            });
        }


        return "redirect:game?cmd=scene";
    }

    /**
     * 挂机执行
     */

    public String beHook(Model model, HttpSession session, RedirectAttributes attr){
        Role role = getSessionRole(session);

        List<BaseMon> baseMons = baseMonMapper.selectList(new LambdaQueryWrapper<BaseMon>().eq(BaseMon::getRoomId, role.getRoomId()));

        Monster npc = NpcServer.getNpc(baseMons.get(0).getId());
        // 怪物信息检测
        if (null == npc || npc.getDead()) {
            Message message = new Message("怪物不存在", "返回游戏", "game?cmd=scene");
            model.addAttribute("message", message);
            return message.getTemplateUrl();
        }
        // 不在怪物所在地图
        if (role.getRoomId() != npc.getRoomId()) {
            return "redirect:game?cmd=main";
        }
        // 死亡状态检测
        if (role.getDead()) {
            Message message = new Message("你已经死亡", "返回游戏", "game?cmd=scene");
            model.addAttribute("message", message);
            return message.getTemplateUrl();
        }
        // 初始化战斗
        Room room = SceneServer.getRoom(npc.getRoomId());
        Battle battle = new Battle(room, BattleType.PVE, role, npc);
        Thread th = new Thread(battle);
        th.start();

        // 更新状态
        npc.setBattle(battle);
        role.setBattle(battle);

        // 战斗信息
        model.addAttribute("npc", npc);
        model.addAttribute("role", role);
        List<String> msg = battle.getBattleList();
        model.addAttribute("msg", msg);

        model.addAttribute("pageTitle", "魔界");

//        // 战斗信息
        List<String> battleMessageList = battle.getBattleList();
        model.addAttribute("battleMessageList", battleMessageList);
        // 属性信息
        Role moRole = (Role) battle.getAttacker();
        Monster moMonster = (Monster) battle.getDefender();
        model.addAttribute("npc", moMonster);
        model.addAttribute("role", moRole);

        attr.addFlashAttribute("battleMessageList", battleMessageList);
        role.setBattle(null);
        return "redirect:game?cmd=scene";
    }


    public void time(Model model){
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
        String format = df.format(new Date());
        model.addAttribute("timeNow", format);
    }
}
